<?php

namespace App\Library\Service;

use App\Library\Logic\ShopUserLogic;
use App\Library\Logic\UserAccountLogic;
use App\Library\Requests\UserAccount\UserAccountAddRequest;
use App\Library\Requests\UserAccount\UserAccountReleaseRequest;
use App\Library\Requests\UserAccount\UserAccountWechatPayRequest;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;

class UserAccountService extends BaseService
{
    /**
     *
     * @return UserAccountLogic
     *
     */
    private function logic(): UserAccountLogic
    {
        if (empty($this->logic)) $this->logic = new UserAccountLogic;
        return $this->logic;
    }

    /**
     * 绑定提现账号
     * @param UserAccountAddRequest $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function add(UserAccountAddRequest $request): JsonResponse
    {
        if ((int)$request->paymentMethodId !== 3) $result = self::addAlipayOrWechatPay($request);
        else $result = self::addBank($request);
        if ($result instanceof JsonResponse) return $result;
        return success();
    }

    /**
     * 绑定支付宝或微信
     * @param $data
     * @return JsonResponse|void
     *
     */
    private function addAlipayOrWechatPay($data)
    {
        $info = $this->logic()->check($data);
        if (!empty($info)) return fail(636);
        $user = ShopUserLogic::query()->where(['user_id' => USERID, 'user_delete_flag' => 2])->first();
        if (is_null($user)) return fail(433);
        $this->logic()->insertForModel([
            'payment_method_id' => $data->paymentMethodId,
            'name' => $user->dm->real_name ?? $data->name,
            'nickname' => $user->user_nickname,
            'identity' => $data->identity,
            'userId' => USERID,
        ]);
    }

    /**
     *
     * @param $data
     * @return JsonResponse
     * @throws GuzzleException
     *
     */
    private function addBank($data): JsonResponse
    {
        $client = new Client;
        $response = $client->post(__('base.bankApi') . $data->identity);
        $res = json_decode($response->getBody()->getContents());
        if ($res->validated === false) return fail(638);
        else if ($res->cardType === 'CC') return fail(639);
        cache(['instId:' . $data->identity => $res->bank]);
        return AuditService::helperAudit($data->identity, 'bank');
    }

    /**
     * 解绑提现账号
     * @param UserAccountReleaseRequest $request
     * @return JsonResponse
     *
     */
    public function release(UserAccountReleaseRequest $request): JsonResponse
    {
        $info = $this->logic()->findByWhere(['serialNumber' => $request->serialNumber]);
        if (is_null($info)) return fail(433);
        $info->delete();
        return success();
    }

    /**
     * 列表
     * @return JsonResponse
     *
     */
    public function list(): JsonResponse
    {
        $info = $this->logic()->getByWhere('userId', USERID);

        return success($info);
    }

    /**
     * 绑定微信提现
     * @param UserAccountWechatPayRequest $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function wechat(UserAccountWechatPayRequest $request): JsonResponse
    {
        $data = $this->baseLoginByCode($request->code, 'mini');
        if ($data instanceof JsonResponse) return $data;
        return success($data);
    }
}
